'''
# -*- coding:utf-8 -*-
# @Project: DevOps_api
# @Date: 2023/3/20 - 21:42
# @Author: ysg
# @Describe: 

alarmtype1.py
'''
import datetime
from libs.alarmspy.dl_mysql import get_dl_mysql
from libs.send_email.emails import s_mails
# from libs.send_tx_sms.sms import send_sms_by_phone

conn, cursor = get_dl_mysql()


def alarm_mysql(ret_dit, variable_list):
    '''
    {'time': '2023-03-20 21:00:48', 'cpu': 0.0, 'memory': 14.4, 'disk': 5.2, 'recvMB': 22, 'sentMB': 13, 'host_ip': '101.132.138.55', 'host_name': '李明轩赞助'}
    [{'var_lable': 'cpu 阀值设置(%)', 'var_name': 'host_CPU', 'var_value': '60'}, {'var_lable': 'dict 阀值设置(%)', 'var_name': 'host_dict', 'var_value': '60'}]
    :param lit:
    :param variable_list:
    :return:
    '''
    alarm_str = ''
    for key, val in variable_list.items():
        if int(ret_dit[key]) >= int(val['var_value']):
            alarm_name = val['var_lable']
            ip_name = f'{ret_dit["host_ip"]}_{ret_dit["host_name"]}'
            alarm_info = f'{ip_name}: 超过阀值 {val["var_value"]},现在值为 {ret_dit[key]}'
            new_time = datetime.datetime.now()
            # 录入数据
            # print('alarm_name', alarm_name)
            # print('alarm_info', alarm_info)
            # print('new_time', new_time)
            # 入库
            cursor.execute(
                f'insert into ops_alarm_info(alarm_name, alarm_info, alarm_time) values("{alarm_name}", "{alarm_info}", "{new_time}")')
            conn.commit()
            # 发送邮箱报警
            alarm_str += f'{alarm_name}: {alarm_info} |||||'
            # s_mails.mails('亦双弓运维管理平台', f'{alarm_name}: {alarm_info}')
            # 使用腾讯云发送报警短信
            # ret = send_sms_by_phone(f'123121', '17326040421')
            # if ret:
            #     print(ret)
            # else:
            #     print('短信发送失败')
    if alarm_str:
        s_mails.mails('亦双弓运维管理平台', alarm_str)


def cron_job_mysql(py_title, alarm_info):
    '''
    :param lit:
    :param variable_list:
    :return:
    '''
    new_time = datetime.datetime.now()
    cursor.execute(
        f'insert into ops_alarm_info(alarm_name, alarm_info, alarm_time) values("{py_title}", "{alarm_info}", "{new_time}")'
    )
    conn.commit()
    if alarm_info:
        s_mails.mails('亦双弓运维管理平台', alarm_info)

